//
//  XMAddressAddViewController.m
//  XunMi
//
//  Created by MacBook on 2024/3/5.
//  Copyright © 2024 TieZhu. All rights reserved.
//

#import "XMAddressAddViewController.h"
#import <BRPickerView/BRPickerView.h>
#import "XMAddressModel.h"

@interface XMAddressAddViewController () <UITextFieldDelegate, UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UIButton *defBtn;
@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *phoneField;
@property (weak, nonatomic) IBOutlet UILabel *addrLa;
@property (weak, nonatomic) IBOutlet UITextView *textView;
@end

@implementation XMAddressAddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initVariables];
    [self setupSubviews];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    /// 激活侧滑返回
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    };
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    /// 禁用侧滑返回
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }
}

#pragma mark - 初始化变量
- (void)initVariables {
    if (self.type == XMAddressHandleTypeEdit) {
        self.defBtn.selected = self.model.is_default;
        self.nameField.text = self.model.recipient;
        self.phoneField.text = self.model.mobile;
        self.addrLa.text = self.model.region;
        self.textView.text = self.model.detail_address;
    }
}
#pragma mark - setupSubViews
- (void)setupSubviews {
    self.navView.backgroundColor = kHEX(@"#F2F2F2");
    self.view.backgroundColor = kHEX(@"#F2F2F2");
    [self.navLeft2Btn setTitle:@"添加收货地址" forState:UIControlStateNormal];
    [self.defBtn setImagePosition:XMImagePositionLeft spacing:6];
    
    self.nameField.delegate = self;
    self.phoneField.delegate = self;
    self.textView.delegate = self;
    self.textView.placeholder = @"小区、写字楼、门牌号等";
    self.textView.placeholderColor = kHEX(@"#B4B4B6");
    self.textView.autocorrectionType = UITextAutocorrectionTypeNo;

}
#pragma mark - Actions
- (IBAction)defAct:(UIButton *)sender {
    sender.selected = !sender.selected;
}
- (IBAction)chooseAdd:(UIButton *)sender {
    [self.view endEditing:YES];
    BRAddressPickerView *addressPickerView = [[BRAddressPickerView alloc] init];
    addressPickerView.pickerMode = BRAddressPickerModeArea;
    addressPickerView.title = @"请选择地区";
    if (self.addrLa.text.length > 0) {
        addressPickerView.selectValues = [self.addrLa.text componentsSeparatedByString:@" "];
    }
    XMWeakSelf(self);
    addressPickerView.resultBlock = ^(BRProvinceModel *province, BRCityModel *city, BRAreaModel *area) {
        XMStrongSelf(self);
        self.addrLa.textColor = k1F1F1F;
        self.addrLa.text = [NSString stringWithFormat:@"%@ %@ %@", province.name, city.name, area.name];
    };
    addressPickerView.pickerStyle = [self pickerStyle];
    [addressPickerView show];
}

- (IBAction)saveAct:(UIButton *)sender {
    if ([XMUtil isBlankString:self.nameField.text]) {
        [MBProgressHUD showHUDMsg:@"请填写收件人姓名!" toView:nil];
        return;
    }
    
    if ([XMUtil isBlankString:self.phoneField.text]) {
        [MBProgressHUD showHUDMsg:@"请填写收件人手机号!" toView:nil];
        return;
    }
    
    if ([XMUtil isBlankString:self.addrLa.text]) {
        [MBProgressHUD showHUDMsg:@"请填写收件人地址!" toView:nil];
        return;
    }
    
    if ([XMUtil isBlankString:self.textView.text]) {
        [MBProgressHUD showHUDMsg:@"请填写收件人详细地址!" toView:nil];
        return;
    }
    [self.view endEditing:YES];
    [MBProgressHUD showHUDActivityMsg:nil toView:kWindow];
    NSMutableDictionary *dic = @{
        @"is_default": @(self.defBtn.selected ? 1 : 0),
        @"recipient": self.nameField.text,
        @"mobile": self.phoneField.text,
        @"region": self.addrLa.text,
        @"detail_address": self.textView.text,
        @"index": self.type == XMAddressHandleTypeAdd ? @"0" : self.model.index
    }.mutableCopy;
    
    [YHNetworkManager.sharedInstance postWithUrl:@"api/address/handle" parameters:dic completeBlock:^(XMResponseTip * _Nonnull tip, id  _Nullable result) {
        if (tip.success) {
            [MBProgressHUD hideHUD];
            if (self.saveBlock) {
                self.saveBlock();
            }
            [self backAction];
        }
    }];
    
}

- (IBAction)nameChange:(UITextField *)sender {
    if (sender.text.length > kUserNameMaxLength) {
        sender.text = [sender.text substringWithRange:NSMakeRange(0, kUserNameMaxLength)];
    }
}

- (IBAction)phoneChange:(UITextField *)sender {
    if (sender.text.length > 11) {
        sender.text = [sender.text substringWithRange:NSMakeRange(0, 11)];
    }
}

- (void)textViewDidChange:(UITextView *)textView {
    // 处理字符变化的逻辑
    if (textView.text.length > 50) {
        textView.text = [textView.text substringWithRange:NSMakeRange(0, 50)];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return [textField resignFirstResponder];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (BRPickerStyle *)pickerStyle {
    BRPickerStyle *customStyle = [[BRPickerStyle alloc] init];
    customStyle.pickerTextColor = kHEX(@"999999");
    customStyle.selectRowTextColor = kAppMainColor;
    customStyle.separatorColor = kAppMainColor;
    customStyle.topCornerRadius = 20;
    customStyle.titleTextFont = kUIFont(16);
    customStyle.titleTextColor = UIColor.blackColor;
    return customStyle;
}
@end
